import { dateFromString } from '@vimi/utils-tool'

/**
 * 计算两个时间差（毫秒）
 * @param {String} startDate 开始时间
 * @param {String} endDate 结束时间
 */
export const deltaDate = (startDate, endDate) => {
  startDate = dateFromString(startDate)
  endDate = dateFromString(endDate)

  if (!startDate || !endDate) {
    return null
  }

  return +endDate - +startDate
}

/**
 * 计算两个时间的间隔天数，相同时间为1天
 * @param {String} startDate 开始时间
 * @param {String} endDate 结束时间
 */
export const deltaDay = (startDate, endDate) => {
  const delta = deltaDate(startDate, endDate)
  return delta !== null ? Math.ceil((delta < 0 ? 0 : delta) / 8.64e7) : null
}

/**
 * 获取月最后一天的日期
 * @param {*} year 年
 * @param {*} month 月
 * @returns {Date}
 */
export const getMonthLastDayDate = (year, month) => {
  if (++month === 12) {
    year++
    month = 0
  }
  return new Date(year, month, 0)
}

export const formatToDay = date => `${date || ''}`.substring(0, 10)

export const lastPickerShortcuts = [
  {
    text: '过去一周',
    onClick(picker) {
      const start = new Date()
      const end = new Date()
      start.setTime(start.getTime() - 8.64e7 * 7)
      picker.$emit('pick', [start, end])
    },
  },
  {
    text: '过去一个月',
    onClick(picker) {
      const start = new Date()
      const end = new Date()
      start.setTime(start.getTime() - 8.64e7 * 30)
      picker.$emit('pick', [start, end])
    },
  },
  {
    text: '过去三个月',
    onClick(picker) {
      const start = new Date()
      const end = new Date()
      start.setTime(start.getTime() - 8.64e7 * 90)
      picker.$emit('pick', [start, end])
    },
  },
]

export const nextPickerShortcuts = [
  {
    text: '未来一周',
    onClick(picker) {
      const start = new Date()
      const end = new Date()
      end.setTime(end.getTime() + 8.64e7 * 7)
      picker.$emit('pick', [start, end])
    },
  },
  {
    text: '未来一个月',
    onClick(picker) {
      const start = new Date()
      const end = new Date()
      end.setTime(end.getTime() + 8.64e7 * 30)
      picker.$emit('pick', [start, end])
    },
  },
  {
    text: '未来三个月',
    onClick(picker) {
      const start = new Date()
      const end = new Date()
      end.setTime(end.getTime() + 8.64e7 * 90)
      picker.$emit('pick', [start, end])
    },
  },
]
